package com.hxh.generater.springBoot.util;

import com.hxh.generater.springBoot.util.methodParse.ProcessRepeatLine;
import com.hxh.generater.springBoot.util.methodParse.entity.MethodBase;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.io.BufferedWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author HXH
 * @description: 处理每一行的写入、空行数量的限制、关键词替换
 * @date 2024/3/12
 **/
@Data
public class ProcessWriteLine {
    private int emptyLineLastLine = 0;

    private Map<String, String> replaceMap;

    private List<? extends MethodBase> methodList;
    private List<? extends MethodBase> methodRepeatList;
    private int maxRepeatMethodPointer;
    private int repeatMethodPointer;
    ProcessRepeatLine processRepeatLine;

    public ProcessWriteLine(Map<String, String> replaceMap) {
        this.replaceMap = replaceMap;
        processRepeatLine = new ProcessRepeatLine();
    }

    public ProcessWriteLine(Map<String, String> replaceMap, List<? extends MethodBase> methodList, List<? extends MethodBase> methodRepeatList) {
        this.replaceMap = replaceMap;
        this.methodList = methodList;
        this.methodRepeatList = methodRepeatList;
        this.maxRepeatMethodPointer = methodRepeatList.size() - 1;
        this.repeatMethodPointer = -1;
        if (!CollectionUtils.isEmpty(methodRepeatList)) {
            this.repeatMethodPointer = 0;
        }
        processRepeatLine = new ProcessRepeatLine();
    }

    public void writeLine(String line, BufferedWriter writer) throws IOException {
        if (StringUtils.isNotBlank(line)) {
            line = replaceLine(line);
            //替换两次 因为有些value是key
            line = replaceLine(line);
            line = processRepeatLine.processOneLine(line);

            writer.write(line);
            writer.newLine();
            emptyLineLastLine = 0;
        } else if (emptyLineLastLine < 1) {
            emptyLineLastLine++;
            writer.newLine();
        }
    }


    public void writeLineWithRepeatMethod(String line, BufferedWriter writer, int currentLine) throws IOException {
        if (repeatMethodPointer >= 0) {
            //有重复方法，忽略
            MethodBase repeatMethod = methodRepeatList.get(repeatMethodPointer);
            if (currentLine >= repeatMethod.getStartLine() && currentLine <= repeatMethod.getEndLine()) {
                if (currentLine >= repeatMethod.getEndLine()) {
                    if (repeatMethodPointer >= maxRepeatMethodPointer) {
                        repeatMethodPointer = -1;
                    } else {
                        repeatMethodPointer++;
                    }
                }
                return;
            } else if (currentLine + 5 >= repeatMethod.getStartLine()) {
                //方法上面的注解也要忽略，5行注解够了吧？
                if (StringUtils.isNotBlank(line)) {
                    String cLine = line.trim().toLowerCase();
                    if (!cLine.startsWith("@autowired") && !cLine.startsWith("@resource")) {
                        if (cLine.startsWith("@") || cLine.startsWith("//")) {
                            return;
                        }
                    }
                }
            }
        }
        writeLine(line, writer);
    }

    /**
     * @param line:
     * @return java.lang.String
     * @author HXH
     * @description: 完成replaceMap中的替换工作
     * @date 2024/3/12
     **/
    private String replaceLine(String line) {
        for (Map.Entry<String, String> stringStringEntry : replaceMap.entrySet()) {
            String regex = ProcessRepeatLine.REPLACE_KEY_WORD + stringStringEntry.getKey() + ProcessRepeatLine.REPLACE_KEY_WORD;
            String replacement = stringStringEntry.getValue();
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(line);
            line = matcher.replaceAll(Matcher.quoteReplacement(replacement));
        }
        return line;
    }


    /**
     * @param line:
     * @param importAddition:
     * @param autowiredAddition:
     * @param writer:
     * @return void
     * @author HXH
     * @description: 把模板中的import语句 / @Autowired/@Resource 语句 提到类前面
     * @date 2024/3/12
     **/
    public static void processPerLine(String line, StringBuilder importAddition, StringBuilder autowiredAddition, BufferedWriter writer) throws IOException {
        if (StringUtils.isNotBlank(line)) {
            if (line.trim().startsWith("import ")) {
                //导包
                importAddition.append(line).append("\n");
            } else if (line.trim().startsWith("@Autowired") || line.trim().startsWith("@Resource") || line.trim().startsWith("@Value")) {
                //注入
                autowiredAddition.append("    " + line).append("\n");
            } else {
                writer.write(line);
            }
        }
        writer.newLine();
    }
}
